<?php
namespace backend\controllers;

use backend\models\Admin;
use backend\models\GroupCat;
use common\components\BaseController;
use SebastianBergmann\CodeCoverage\Report\Xml\Project;
use Yii;
use yii\db\Query;
use \common\helps\tools;
use \common\helps\code;

/**
 * User controller
 */
class ActiveController extends BaseController
{

    public $modelClass = 'backend\models\Task';

    /*
     * 任务列表
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionTaskList()
    {
        $model = new \backend\models\Task();
        $request =$this->request;
        $params = $request->queryParams;
        //$params = $request->bodyParams;
        $total = $model::selectData($params);
        $res = tools::getPageData($total,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','标题','描述','积分','排序'];
            $data = [];
            foreach ($total as $k => $v){
                $data[$k]['id'] = $v['id'];
                $data[$k]['title'] = $v['title'];
                $data[$k]['describe'] = $v['describe'];
                $data[$k]['integral'] = $v['integral'];
                $data[$k]['sort'] = $v['sort'];
            }
            tools::ToPHPEXECL($tableHeader,$data,'任务列表');
        }
        return code::success($res);
    }

    /*
     * 任务详情
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionTaskDetail(){
        $model = new \backend\models\Task();
        $request =$this->request;
        $params = $request->bodyParams;
        $data = $model::findOneData($params['id']);
        return code::success($data);
    }

    /*
     * 修改任务
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionSaveTask()
    {
        $model = new \backend\models\Task();
        $request =$this->request;
        $params = $request->bodyParams;
        $id = $params['id'];
        unset($params['id']);
        $res = $model::saveData($id,$params);
        return code::success($res);
    }

    /*
     * 福利列表
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionWealList()
    {
        $model = new \backend\models\Weal();
        $request =$this->request;
        $params = $request->queryParams;
        $total = $model::selectData($params);
        $res = tools::getPageData($total,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','标题','积分条件','排序','状态'];
            $data = [];
            foreach ($total as $k => $v){
                $data[$k]['id'] = $v['id'];
                $data[$k]['title'] = $v['title'];
                $data[$k]['integral'] = $v['integral'];
                $data[$k]['sort'] = $v['sort'];
                if($v['status'] == 1){
                    $status = '停用';
                }else{
                    $status = '启用';
                }
                $data[$k]['status'] = $status;
            }
            tools::ToPHPEXECL($tableHeader,$data,'福利列表');
        }
        return code::success($res);
    }

    /*
     * 添加福利
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionAddWeal()
    {
        $model = new \backend\models\Weal();
        $request =$this->request;
        $params = $request->bodyParams;
        if(!empty($params)) {
            if ($model->load($params, '')) {
                if ($model->validate()) {
                    $res = $model->saveInfo('create',$params);
                    if(empty($res->errorInfo)){
                        return code::success('');
                    }else{
                        return code::error('error');
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }
        }
    }

    /*
     * 福利详情
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionWealDetail(){
        $model = new \backend\models\Weal();
        $request =$this->request;
        $params = $request->bodyParams;
        $data = $model::findOneData($params['id']);
        return code::success($data);
    }

    /*
     * 修改福利
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionSaveWeal()
    {
        $model = new \backend\models\Weal();
        $request =$this->request;
        $params = $request->bodyParams;
        $id = $params['id'];
        unset($params['id']);
        if(!empty($params)) {
            if ($model->load($params, '')) {
                if ($model->validate()) {
                    $res = $model->saveInfo('update',$params,$id);
                    if(empty($res->errorInfo)){
                        return code::success('');
                    }else{
                        return code::error('error');
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }
        }else{
            $data = $model::findOneData($id);
            return code::success($data);
        }
    }

    /*
     * 删除福利
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionDeleteWeal()
    {
        $model = new \backend\models\Weal();
        $request =$this->request;
        $params = $request->bodyParams;
        $res = $model::deleteData($params['id']);
        return code::success($res);
    }

    /*
     * 福利兑换列表
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionWealExchangeList()
    {
        $model = new \backend\models\WealExchange();
        $request =$this->request;
        $params = $request->queryParams;
        $total = $model::selectData($params);
        $res = tools::getPageData($total,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','会员昵称','手机号','福利id','福利标题','状态','备注'];
            $data = [];
            foreach ($total as $k => $v){
                $data[$k]['id'] = $v['id'];
                $data[$k]['username'] = $v['username'];
                $data[$k]['mobile'] = $v['mobile'];
                $data[$k]['weal_id'] = $v['weal_id'];
                $data[$k]['weal_title'] = $v['weal_title'];
                if($v['status'] == 1){
                    $status = '待发放';
                }else{
                    $status = '已发放';
                }
                $data[$k]['status'] = $status;
                $data[$k]['mark'] = $v['mark'];
            }
            tools::ToPHPEXECL($tableHeader,$data,'福利兑换列表');
        }
        return code::success($res);
    }

    /*
     * 发放福利
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionSetWeal()
    {
        $model = new \backend\models\WealExchange();
        $request =$this->request;
        $params = $request->bodyParams;
        $res = $model::saveData($params);
        return code::success($res);
    }

}
